package org.example.service.pc;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.example.R.R;
import org.example.entity.dto.AddAssQuestionDto;
import org.example.entity.dto.QueryQuestionDto;
import org.example.entity.po.AssQuestionPO;
import org.example.entity.vo.admin.UserVO;
import org.example.entity.vo.pc.AssQuestionVO;
import org.example.mapper.AssQuestionMapper;
import org.example.mapper.UserMapper;
import org.example.utils.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author sunyaoyao
 * @Date 2024/7/28 17:10
 */
@Service
public class AssQuestionService {

    @Autowired
    private AssQuestionMapper assQuestionMapper;

    @Autowired
    private UserMapper userMapper;

    public R add(AddAssQuestionDto dto) {

        AssQuestionPO po = new AssQuestionPO();
        BeanUtils.copyProperties(dto, po);
        po.setId(IdUtil.getSnowflakeNextId());
        po.setCreateDate(TimeUtils.getCurrentDate());
        po.setStatus(0);

        assQuestionMapper.insert(po);
        return R.success("提交成功");
    }


    public R list() {
        List<AssQuestionVO> result = new ArrayList<>();
        List<AssQuestionVO> list = assQuestionMapper.list();

        List<UserVO> userList = userMapper.list();
        Map<Long, String> userMap = userList.stream().collect(Collectors.toMap(UserVO::getId, UserVO::getNickname));
        Map<Long, Integer> userStatusMap = userList.stream().collect(Collectors.toMap(UserVO::getId, UserVO::getStatus));
        for (AssQuestionVO vo : list) {
            AssQuestionVO assQuestionVO = new AssQuestionVO();
            BeanUtils.copyProperties(vo, assQuestionVO);
            if (vo.getUserId() != null) {
                assQuestionVO.setUserName(userMap.get(Long.valueOf(vo.getUserId())));
            }
            if (vo.getTaskUserId() != null) {
                assQuestionVO.setTaskUserName(userMap.get(Long.valueOf(vo.getTaskUserId())));
            }
            result.add(assQuestionVO);
        }
        result.sort(Comparator.comparing(AssQuestionVO::getCreateDate).reversed());
        return R.success("获取成功!").setData(result);
    }

    public R delete(Long id) {
        assQuestionMapper.delete(id);
        return R.success("删除成功!");
    }

    public R lisByRole(QueryQuestionDto dto) {
        List<AssQuestionVO> list = new ArrayList<>();
        if (dto.getStatus() == 111) {
            list = assQuestionMapper.listUser(dto);
        } else if (dto.getStatus() == 222) {
            list = assQuestionMapper.listDy(dto);
        }
        list.sort(Comparator.comparing(AssQuestionVO::getCreateDate).thenComparing(AssQuestionVO::getStatus).reversed());
        return R.success("获取成功").setData(list);
    }

    public R overTask(Long id) {
        assQuestionMapper.overTask(id);
        return R.success("任务完成!");
    }
}
